import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int t = in.nextInt();
        in.nextLine();
        for (int i = 0; i < t; i++) {
            int n = in.nextInt();
            int k = in.nextInt();
            in.nextLine();
            String str = in.nextLine();
            System.out.println(solution(n,k,str));
        }
    }

    private static int solution(int n, int k, String str) {
        int count = 0;
        int del = 0;
        for (int i = 0; i < n; i++) {
            char c = str.charAt(i);
            if(c == 'L'){
                del--;
            }else {
                if(i >= 2 && str.charAt( i - 1) == 'W' && str.charAt(i - 2) == 'W'){
                    del += k;
                }else {
                    del++;
                }
            }
        }
        return del;
    }

}